using System;
using UnityEngine;

public class ParticleUnit
{
	public float startTime;

	public float endTime;

	public float maxLife;

	public Vector3 velocity;

	public Vector2 scale;

	public Vector3 distanceFromParticle2D = Vector3.zero;

	public Vector2 generatedPosition = Vector2.zero;

	public Vector2 localPosition = Vector2.zero;

	public Quaternion localRotation = Quaternion.identity;

	public Vector2 localScale = Vector2.one;

	public UISpriteData spriteData;

	public Rect texCoordsOuter;

	public Rect texCoordsInner;

	public Color color = Color.white;

	private Vector3 startPoint;

	public float ortoDistance;

	public void CreateParticleUnit(Particle2D particle, float curTime)
	{
		Texture texture = particle.atlas.texture;
		spriteData = particle.atlas.GetSprite(particle.GetSpriteNameRandomly());
		Rect rect = new Rect(spriteData.x, spriteData.y, spriteData.width, spriteData.height);
		Rect rect2 = new Rect(spriteData.x + spriteData.borderLeft, spriteData.y + spriteData.borderTop, spriteData.width - spriteData.borderLeft - spriteData.borderRight, spriteData.height - spriteData.borderBottom - spriteData.borderTop);
		texCoordsOuter = NGUIMath.ConvertToTexCoords(rect, texture.width, texture.height);
		texCoordsInner = NGUIMath.ConvertToTexCoords(rect2, texture.width, texture.height);
		if (particle.emitter == Particle2D.Emitter.RECT)
		{
			float num = particle.emitterWidth * 0.5f;
			float num2 = particle.emitterHeight * 0.5f;
			generatedPosition = new Vector3(UnityEngine.Random.Range(0f - num, num), UnityEngine.Random.Range(0f - num2, num2), 0f);
		}
		else if (particle.emitter == Particle2D.Emitter.CIRCLE)
		{
			float num3 = particle.emitterWidth * 0.5f;
			float num4 = particle.emitterHeight * 0.5f;
			float num5 = UnityEngine.Random.Range(0f - num3, num3);
			float num6 = UnityEngine.Random.Range(0f - num4, num4);
			float f = UnityEngine.Random.Range(0f, (float)Math.PI);
			float x = Mathf.Cos(f) * num5;
			float y = Mathf.Sin(f) * num6;
			generatedPosition = new Vector3(x, y, 0f);
		}
		else
		{
			generatedPosition = Vector3.zero;
		}
		startPoint = particle.transform.position;
		maxLife = UnityEngine.Random.Range(particle.minLifeTime, particle.maxLifeTime);
		float num7 = UnityEngine.Random.Range(particle.minSize, particle.maxSize);
		scale = new Vector2(num7, num7);
		if (particle.useRotation)
		{
			localRotation = Quaternion.Euler(new Vector3(0f - UnityEngine.Random.Range(particle.minRotation.x, particle.maxRotation.x), 0f - UnityEngine.Random.Range(particle.minRotation.y, particle.maxRotation.y), 0f - UnityEngine.Random.Range(particle.minRotation.z, particle.maxRotation.z)));
		}
		else
		{
			localRotation = Quaternion.identity;
		}
		velocity = Quaternion.AngleAxis(UnityEngine.Random.Range(particle.minAngle, particle.maxAngle), Vector3.back) * new Vector3(0f, UnityEngine.Random.Range(particle.minPower, particle.maxPower), 0f);
		startTime = curTime;
		endTime = startTime + maxLife;
		ortoDistance = UnityEngine.Random.Range(particle.ortoDistanceMin, particle.ortoDistanceMax);
	}

	public bool UpdateParticleUnit(Particle2D particle, float curTime, float curDeltaTime)
	{
		if (curTime >= endTime)
		{
			particle.AddRemovableObject(this);
			return false;
		}
		float num = 1f - (endTime - curTime) / maxLife;
		if (particle.useSizeRate)
		{
			localScale = scale * particle.sizeRate.Evaluate(num);
		}
		else
		{
			localScale = Vector2.one;
		}
		if (particle.worldSpace)
		{
			distanceFromParticle2D = startPoint - particle.transform.position;
		}
		else
		{
			distanceFromParticle2D = Vector3.zero;
		}
		if (particle.useVelocity)
		{
			float num2 = curTime - startTime;
			Vector2 vector = velocity;
			if (particle.useAcceleration)
			{
				vector *= particle.accelerationRate.Evaluate(num);
			}
			if (particle.useForce)
			{
				localPosition = generatedPosition + vector * num2 + particle.force * Mathf.Pow(num2, 2f);
			}
			else
			{
				localPosition = generatedPosition + vector * num2;
			}
		}
		else
		{
			localPosition = generatedPosition;
		}
		if (particle.useRotation && particle.useRotationRate)
		{
			localRotation *= Quaternion.Euler(new Vector3((0f - particle.rotationRateX.Evaluate(num)) * 10f, (0f - particle.rotationRateY.Evaluate(num)) * 10f, (0f - particle.rotationRateZ.Evaluate(num)) * 10f));
		}
		if (particle.useAnimatedColor)
		{
			color = particle.GetColor(num);
		}
		else
		{
			color = Color.white;
		}
		if (particle.useGoToPosition && particle.travelTo != null)
		{
			localPosition = Vector2.Lerp(generatedPosition, particle.travelToPosition, particle.travelRate.Evaluate(num)) + particle.ortoDirection * particle.ortoRate.Evaluate(num) * ortoDistance;
		}
		return true;
	}
}
